package cn.lyd.controller;

import cn.lyd.domain.Admin;
import cn.lyd.dto.AdminDto;
import cn.lyd.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/system")//整个后台的接口
public class SystemController {

    @Autowired
    private IAdminService adminService;

    //首页
    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    //注册页 Get请求
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String register(){
        return "register";
    }

    //登录页 Get请求
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "login";
    }

    //修改密码页面 Get请求
    @RequestMapping(value = "/updatePwd",method = RequestMethod.GET)
    public String updatePwd() {
        return "updatePwd";
    }

    //验证登录 Post
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(Admin admin, Model model, HttpSession session){
        //数据库查询是否有该用户的信息
        Admin a2 = adminService.loadOne(admin);
        if (a2!=null) {//用户存在
            //保存用户信息，session
            session.setAttribute("INFO",a2);
            //去首页
            return "index";
        } else {//用户不存在
            //提示错误信息
            model.addAttribute("msg","用户名或者密码错误");
            //返回到登录页
            return "login";
        }
    }

    //注册 Post
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String register(Admin admin, Model model){

        //数据库查询是否有该账号是否已被注册
        Admin account = adminService.findAccount(admin.getUsername());
        if (account!=null) {//账号存在
            //提示错误信息
            model.addAttribute("msg","该账号已被注册，请重新输入！");
            //返回注册页
            return "register";

        } else {//账号不存在
            //添加账号
           adminService.addOne(admin);
           //提升账号注册成功
           model.addAttribute("msg","注册成功！");
            //返回到登录页
            return "login";
        }
    }

    //修改密码 Post
    @RequestMapping(value = "/updatePwd",method = RequestMethod.POST)
    public String updatePwd(AdminDto adminDto,HttpServletRequest request, Model model){

        //获取session用户信息
        Admin info = (Admin) request.getSession().getAttribute("INFO");
        //证明是本人在登录状态修改
        //账号一致
        boolean equals1 = info.getUsername().equals(adminDto.getUsername());
        //密码一致
        boolean equals2 = info.getPassword().equals(adminDto.getPassword());
        //新旧密码一致
        boolean equals3 = info.getPassword().equals(adminDto.getNewpassword());

        //不满足账号或者密码与当前登录用户一致，
        if (!equals1||!equals2){
            model.addAttribute("msg","账号或密码不正确！");
            return "updatePwd";
        }

        if (equals3){
            model.addAttribute("msg","新旧密码不能一致！");
            return "updatePwd";
        }

        //都没有进入if，修改成功
        adminService.updatePwd(adminDto);
        //清除session
        request.getSession().setAttribute("INFO",null);
        model.addAttribute("msg","修改成功，请重新登录！");
        return "login";


       /* //数据库查询该账号是否存在
        Admin admin = adminService.findAccount(adminDto.getUsername());
        if (admin!=null) {//账号存在

            //对比输入的账号和初始密码是否和session中的一致
            Admin info = (Admin) request.getSession().getAttribute("INFO");
            boolean equals1 = info.getUsername().equals(adminDto.getUsername());
            boolean equals2 = info.getPassword().equals(adminDto.getPassword());
            //新旧密码一致？
            boolean equals3 = info.getPassword().equals(adminDto.getNewpassword());

            if (!equals1 ){//账号不对应，非本人修改
                //提示信息
                model.addAttribute("msg","非本人修改！");
                //返回修改密码页面
                return "updatePwd";
            }
            if (!equals2){
                //提示信息
                model.addAttribute("msg","初始密码错误！");
                //返回修改密码页面
                return "updatePwd";
            }
            if (equals3){
                //提示信息
                model.addAttribute("msg","新旧密码不能一样！");
                //返回修改密码页面
                return "updatePwd";
            }
            //如果没有进入上面3个判断

            //退出登录
            request.getSession().setAttribute("INFO",null);

            //提示信息
            model.addAttribute("msg","修改成功，请重新登录！");

            //返回注册页
            return "login";

        } else {//账号不存在
            //提升账号不存在
            model.addAttribute("msg","该账号不存在！");
            //返回到登录页
            return "login";
        }*/
    }



    //退出登录
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("INFO",null);
        return "login";
    }





}
